package org.hxyjs.dao.impl;

import org.hxyjs.dao.OthersDao;
import org.hxyjs.entity.Others;
import org.hxyjs.mapper.OthersMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("othersDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class OthersDaoImpl implements OthersDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private OthersMapper othersMapper;
    public List<Others>  getAllOtherss() throws Exception{
        return othersMapper.selectByExample(null);
    }

    @Override
    public List<Others> getOthersByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) othersMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return othersMapper.getOthersByPage(map);
    }
    @Override
    public Others selectOthersBySno(String ono) throws Exception {
        return othersMapper.selectByPrimaryKey(ono);
    }
    @Override
    public Integer deleteOthersByOno(String ono) {
        return othersMapper.deleteByPrimaryKey(ono);
    }

    @Override
    public Integer updateOthersByOno(Others others) {
        return othersMapper.updateByPrimaryKey(others);
    }
    @Override
    public Integer addOthersByOno (Others others) {
        others.setOno(initId());
        return othersMapper.insertSelective(others);
    }

    public String initId() {
        String maxOno = othersMapper.getMaxOno();
        return GenNumTools.initId("", 4, maxOno);
    }
    @Override
    public Others getOthersByOno(String ono) throws Exception {
        return othersMapper.selectByPrimaryKey(ono);
    }
}